package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.*;
import com.xzlcPT.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("PostionCollect")
@SessionAttributes({"userLogin"})
public class PostionCollectController {
    @Autowired
    private XzPostionCollectService postionCollectService;
    @Autowired
    private XzPostionService postionService;
    @Autowired
    private LoginUserService loginService;
    @Autowired
    private XzResumeService xzResumeService;
    @Autowired
    private XzPostionSendService postionSendService;

    //我收藏的职位
    @ResponseBody
    @RequestMapping("selCollectByMemberId.do")
    public Map selCollectByMemberId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows, @ModelAttribute("userLogin")XzLogin login){
        Map map = new HashMap();
        PageBean<XzPostionCollect> pageBean = postionCollectService.selCollectByMemberId(page,rows,login.getMember().getMemberId());
        map.put("postionCollectList",pageBean.getList());
        map.put("page", pageBean.getPageNum());
        map.put("pages", pageBean.getPages());
        map.put("rows", pageBean.getPageSize());
        map.put("total", pageBean.getTotal());
        return map;
    }
    //查询是否收藏
    @ResponseBody
    @RequestMapping("selCollectState.do")
    public Map selCollectState(@ModelAttribute("userLogin") XzLogin userLogin,Long postionId){
        Map map=new HashMap();
        map.put("memberId",userLogin.getMember().getMemberId());
        map.put("postionId",postionId);
        int i=postionCollectService.selCollectState(map);
        Map map1=new HashMap();
        if(i==1){
            map1.put("msg","ok");
        }else {
            map1.put("msg","err");
        }
        return map1;
    }
    //收藏职位
    @ResponseBody
    @RequestMapping("insertCollect.do")
    public Map insertCollect(@ModelAttribute("userLogin") XzLogin userLogin,Integer postionId){
        Date collectTime=new Date();
        Map map=new HashMap();
        map.put("postionId",postionId);
        map.put("memberId",userLogin.getMember().getMemberId());
        map.put("collectTime",collectTime);
        int i=postionService.insertCollect(map);
        if(i==1){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }
    //删除收藏
    @ResponseBody
    @RequestMapping("deleteCollect.do")
    public Map deleteCollect(Long collectId){
        int i=postionService.deleteCollect(collectId);
        Map map=new HashMap();
        if(i==1){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }

    //移动端方法----------------------------------------------------------
    //我收藏的职位
    @ResponseBody
    @RequestMapping("selCollectByMemberId.go")
    public Map selCollectByMemberId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows, @RequestHeader("accessToken") String token,String insertSelectivemsg){
        Map map = new HashMap();
        XzLogin xzLogin =loginService.selByToken(token);
        PageBean<XzPostionCollect> pageBean = postionCollectService.selCollectByMemberId(page,rows,xzLogin.getMember().getMemberId());
        if(insertSelectivemsg!=""){
            map.put("insertSelectivemsg",insertSelectivemsg);
        }
        map.put("postionCollectList",pageBean.getList());
        map.put("page", pageBean.getPageNum());
        map.put("pages", pageBean.getPages());
        map.put("rows", pageBean.getPageSize());
        map.put("total", pageBean.getTotal());
        return map;
    }

    //简历投递
    @RequestMapping("sendPostionMob.go")
    public ModelAndView sendPostionMob(Long postionId,@RequestHeader("accessToken") String token){
        ModelAndView mv = new ModelAndView("forward:selCollectByMemberId.go");
        XzLogin xzLogin =loginService.selByToken(token);
        XzResume resume = xzResumeService.selectByMemberId(xzLogin.getMember().getMemberId());
        XzPostion postion=postionService.selPostionInfo(postionId);
        Long companyId=postion.getCompanyId();
        Long resumeId=resume.getResumeId();
        Date date=new Date();
        XzPostionSend xzPostionSend=new XzPostionSend();
        xzPostionSend.setResumeId(resumeId);
        xzPostionSend.setPostionId(postionId);
        xzPostionSend.setCompanyId(companyId);
        xzPostionSend.setSendTime(date);
        xzPostionSend.setCreateTime(date);
        xzPostionSend.setSendTime1(date);
        int i=postionSendService.insertSelective2(xzPostionSend,xzLogin);
        if(i==1){
            mv.addObject("insertSelectivemsg","ok");
        }else{
            mv.addObject("insertSelectivemsg","err");
        }
        return mv;
    }
}
